import axios from "axios";
import { Message } from "element-ui";
import store from "@/store";
import router from "@/router";
const request = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
request.interceptors.request.use(
  (config) => {
    config.headers.Authorization = store.getters.token;
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (err) => {
    if(err.response.status === 401) {
      Message.error('登录过期，请重新登录')
      router.push('/login')
    } else {
      Message.error(err.response.data.message)
    }
    return Promise.reject(err);
  }
);
export default request;
